<template>
	<view class="container">
		<uni-list>
			<uni-list-item
				v-for="item in signHistory"
				showArrow
				link="navigateTo"
				:to="`/pages/sign/sign?activityId=${activityId}&activityName=${activityName}&stage=${item.stage}&count=${item.count}`"
				:title="`第${item.stage}阶段 第${item.count}次签到`"
				:rightText="item.sign"
			/>
		</uni-list>
		<uni-load-more iconType="circle" :status="status" @clickLoadMore="getMoreSign" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			status: 'more',
			pageNum: 1,
			pageSize: 5,
			apiUrl: 'http://localhost:8080/',
			activityId: 1,
			activityName: '',
			signHistory: []
		};
	},
	onLoad: function(option) {
		this.activityId = option.activityId;
		this.getTitleByActivityId();
		uni.setNavigationBarTitle({
			title: this.activityName
		});
		this.getSignHistory();
	},
	onPullDownRefresh() {
		if (this.status == 'more') {
			this.pageNum = 1;
			this.getSignHistory();
		}
	},
	methods: {
		onClick(e) {
			console.log('执行click事件', e.data);
			uni.showToast({
				title: '点击反馈'
			});
		},
		getMoreSign() {
			this.pageNum++;
			this.getSignHistory();
		},
		getTitleByActivityId() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'activity/' + this.activityId,
				header: {
					// Authorization: uni.getStorageSync('token')
				},
				data: {},
				success: res => {
					console.log(res.data);
					this.activityName = res.data.name;
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getSignHistory() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'sign/' + this.activityId,
				header: {
					// Authorization: uni.getStorageSync('token')
				},
				data: {
					pageNum: this.pageNum,
					pageSize: this.pageSize
				},
				success: res => {
					console.log(res.data);
					if (this.pageNum <= 1) {
						this.signHistory = [];
					}
					let originSize = this.signHistory.length;
					this.signHistory.push.apply(this.signHistory, res.data.records);
					let deltaSize = res.data.records.length;
					if (res.data.total <= originSize + deltaSize) {
						this.status = 'noMore';
					} else {
						this.status = 'more';
					}
					uni.stopPullDownRefresh();
				},
				fail: err => {
					this.pageNum -= 1;
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		}
	}
};
</script>

<style>
$uni-success: #18bc37 !default;

.container {
	padding: 20px;
	font-size: 14px;
	line-height: 24px;
}
</style>
